      $ SET SOURCEFORMAT "FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID.  InventorySeq2Index.
AUTHOR.  Raj Akula.
* Creates an indexed file  from a sequential file.

*Declare Enviorment details
ENVIRONMENT DIVISION.

*Decalre opening with read and write option files,
* again like the indexed file i take the inventory file
* open it in line sequential and make an indexed file out 
* of it, and that indexed file has a key. 
INPUT-OUTPUT SECTION.
FILE-CONTROL.
   SELECT InventoryFile ASSIGN TO "IdxInventory.dat"
          ORGANIZATION IS INDEXED   
          ACCESS MODE IS RANDOM
          RECORD KEY IS ProductNo
          ALTERNATE RECORD KEY IS ProductDesc
                      WITH DUPLICATES
          FILE STATUS IS InventoryStatus.

   SELECT SeqInventoryFile ASSIGN TO "inventory.dat"
        ORGANIZATION IS LINE SEQUENTIAL.
      
*Declare data like file read line format 
DATA DIVISION.
FILE SECTION.
FD InventoryFile.
01 InventoryRecord.
   02 ProductNo			      PIC 9(6).
   02 Filler                  PIC X(5).
   02 ProductDesc			  PIC X(25).
   02 Stock                   PIC 99.
   02 Filler                  PIC X(6).
   02 MinStock                PIC 99.
   02 Filler                  PIC X(6).
   02 Price                   PIC 99V99.


FD SeqInventoryFile.
01 SeqInventoryRecord.
   88   EndOfFile VALUE HIGH-VALUES.
   02 SeqProductNo		      PIC 9(6).
   02 Filler                  PIC X(5).
   02 SeqProductDesc		  PIC X(25).
   02 SeqStock                PIC 99.
   02 Filler                  PIC X(6).
   02 SeqMinStock             PIC 99.
   02 Filler                  PIC X(6).
   02 SeqPrice                PIC 99V99.


*Local Variables for the program needed, again i use this 
* variable to make sure my write worked correctly. 
WORKING-STORAGE SECTION.
01   InventoryStatus          PIC X(2).

*Procedure to convert sequential file to an indexed file
*Basically i loop throught the file and create an index file
*based on the key ProductNo. In my declarations i already declared
*the key to be productNo. 
PROCEDURE DIVISION.
Begin.
   OPEN INPUT SeqInventoryFile.
   OPEN OUTPUT InventoryFile.

   READ SeqInventoryFile
      AT END SET EndOfFile TO TRUE
   END-READ.
   PERFORM UNTIL EndOfFile
      WRITE InventoryRecord FROM SeqInventoryRecord
         INVALID KEY DISPLAY "VIDEO STATUS :- ", InventoryStatus
      END-WRITE
      READ SeqInventoryFile
         AT END SET EndOfFile TO TRUE
      END-READ
   END-PERFORM.

   CLOSE InventoryFile, SeqInventoryFile.
   STOP RUN.
